﻿using Newtonsoft.Json.Linq;
using RenterClient.Interfaces;
using RenterClient.Models;
using RenterClient.Service;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace RenterClient.Controllers
{
    public class HomeController : Controller
    {
        IRenterService _service = new RenterService();
        private string connectId = string.Empty;

        public async Task<ActionResult> Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            //IRenterService service = new RenterService();
            List<PostModels> posts = await _service.GetAllPosts();

            return View(posts);
        }

        public async Task<ActionResult> RentDetails(string id)
        {

            ViewBag.Message = "Details of the rent deals.";
            PostModels post = null;

            if (Request.Cookies["connect.sid"] != null)
            {
                connectId = Request.Cookies["connect.sid"].Value; 
                post = await _service.GetPost(id, connectId);
            }


            if (post == null)
            {
                return RedirectToAction("Login", "Account");
            }

            return View(post);
        }

        public async Task<ActionResult> GetSearchedPosts(PlaceModels place)
        {
            List<PostModels> posts = await _service.GetSearchedPosts(place);

            return View(posts);
        }


        public async Task<ActionResult> SubmitInfo(PlaceModels place)
        {
            List<PostModels> posts = await _service.GetSearchedPosts(place);
            return View(posts);
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
